package tech.aistar.corejava.day15;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:死锁
 * @date 2019/8/3 0003
 */
public class DeadLockDemo {
    //抢占资源

    //静态域容易产生死锁
     static Object oo1 = new Object();
     static Object oo2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new T1();
        Thread t2 = new T2();

        t1.start();

        t2.start();
    }
}

class T1 extends Thread{
    private DeadLockDemo dd = new DeadLockDemo();

    @Override
    public void run() {
        synchronized (dd.oo1){//拥有临界资源dd.oo1的"锁标记" - 执行同步代码块
            System.out.println("======11=======");

            try {
                //让另外一个线程拥有足够的时间去抢到dd.oo2
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (dd.oo2){
                System.out.println("====12====");
            }
        }
    }
}

class T2 extends Thread{
    private DeadLockDemo dd = new DeadLockDemo();

    @Override
    public void run() {
        synchronized (dd.oo2){
            System.out.println("===21===");

            try {
                ////让另外一个线程拥有足够的时间去抢到dd.oo1
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (dd.oo1){
                System.out.println("===22====");
            }
        }
    }
}
